package com.jysz.certificate.web.action.workflow;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.jysz.certificate.action.AbstractBaseAction;
import com.jysz.certificate.persistence.enums.WorkflowOperateType;
import com.jysz.certificate.persistence.enums.WorkflowState;
import com.jysz.certificate.persistence.enums.WorkflowType;
import com.jysz.certificate.persistence.model.cm.Certificate;
import com.jysz.certificate.persistence.model.cm.CertificateRenew;
import com.jysz.certificate.persistence.model.cm.Certification;
import com.jysz.certificate.persistence.model.cm.Release;
import com.jysz.certificate.persistence.model.cm.applyAdd.ApplyCer;
import com.jysz.certificate.persistence.model.cs.TheNetwork;
import com.jysz.certificate.persistence.model.workflow.Workflow;
import com.jysz.certificate.persistence.model.workflow.WorkflowObject;
import com.jysz.certificate.service.cm.CertificateRenewService;
import com.jysz.certificate.service.cm.CertificateService;
import com.jysz.certificate.service.cm.CertificationService;
import com.jysz.certificate.service.cm.ReleaseService;
import com.jysz.certificate.service.cm.applyCer.ApplyCerService;
import com.jysz.certificate.service.cs.TheNetworkService;
import com.jysz.certificate.service.workflow.WorkflowService;
import com.jysz.certificate.utils.CommonUtil;
import com.jysz.webcore.ssh.orm.hibernate.support.Page;

/**
 * 流程Action
 * @author xuxiaolong
 */
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Namespace("/workflow")
public class WorkflowAction extends AbstractBaseAction{

	private static final long serialVersionUID = 17343375121738566L;

	@Resource
	private WorkflowService wfService;
	@Resource
	private ReleaseService releaseService;
	@Resource
	private CertificationService certificationService;
	@Resource
	private CertificateService certificateService;
	@Resource
	private TheNetworkService theNetworkService;
	@Resource
	private ApplyCerService applyCerService;
	@Resource
	private CertificateRenewService certificateRenewService;
	
	private Workflow search;
	private Long []ids;
	private String type2;
	private Integer status;
	private Integer type;
	private Integer serachIndex = 0;
	
	//列表
	@Action(INDEX)
	public String index(){
		if(type!=null&&type>=0){
			if(search==null)
				search=new Workflow();
			search.setType(WorkflowType.index(type));
		}
		if(status!=null&&status>=0){
			if(search==null)
				search=new Workflow();
			search.setState(WorkflowState.index(status));
		}
		if(serachIndex==2)
			this.setPage(wfService.findPage(getUser(), search,this.getCurrentPage(), this.getPageSize()));
		else
			this.setPage(null);
		return INDEX;
	}
	public Long getFirstObjId(Long wfId){
		
		Workflow wfrkflow=wfService.find(wfId);
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			return list.get(0).getObjectId();
		return null;
		
	}
	
	//准备审核
	@Action("preAudit")
	public String preAudit(){
		search=wfService.find(search.getId());
		return "audit";
	}
		
	//审核流程
	@Action("audit")
	public String audit(){
		WorkflowOperateType ot=null;
		if("pass".equals(type))
			ot=WorkflowOperateType.PASS;
		else
			ot=WorkflowOperateType.REFUSE;
		wfService.audit(search.getId(), ids, ot);
		this.setSuccessPopMessage("流程操作成功");
		setRedirectUrl("list.do?currentPage="+getCurrentPage());
		return SHOW_MSG_PAGE;
	}
	/**
	 * 释放数据
	 * @param id
	 * @return
	 */
	public Release ShopNameMark(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		Release release = releaseService.find(jooid);
		return release;
	}
	/**
	 * 补录
	 * @param id
	 * @return
	 */
	public Certification certificationName(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		return certificationService.find(jooid);
	}
	/**
	 * 修改
	 * @return
	 */
	public Certificate certificateName(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		return certificateService.find(jooid);
	}
	/**
	 * 二网
	 * @return
	 */
	public TheNetwork theNetworkName(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		return theNetworkService.find(jooid);
	}
	public ApplyCer applyCer(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		return applyCerService.find(jooid);
	}
	/**
	 * 换证
	 * @return
	 */
	public CertificateRenew certificateRenewName(Long id){
		Workflow wfrkflow=wfService.find(id);
		Long jooid = 0L;
		List<WorkflowObject> list=wfrkflow.getObjects();
		if(!CommonUtil.isEmptyList(list))
			jooid = list.get(0).getObjectId();
		return certificateRenewService.find(jooid);
	}

	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Workflow getSearch() {
		return search;
	}

	public void setSearch(Workflow search) {
		this.search = search;
	}
	public Integer getSerachIndex() {
		return serachIndex;
	}
	public void setSerachIndex(Integer serachIndex) {
		this.serachIndex = serachIndex;
	}
}
